home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CUJ9207.ARJ / 1007020A < prev    next >
Text File  |  1992-06-03  |  1KB  |  58 lines

  1. Listing 7 -- xwctomb.c
  2.  
  3. /* _Wctomb function */
  4. #include <limits.h>
  5. #include <stdlib.h>
  6. #include "xstate.h"
  7.  
  8. int _Wctomb(char *s, wchar_t wcin, char *ps)
  9.     {    /* translate widechar to multibyte */
  10.     static const char initial = 0;
  11.  
  12.     if (s == NULL)
  13.         {    /* set initial state */
  14.         *ps = initial;
  15.         return (_Mbstate._Tab[0][0] & ST_STATE);
  16.         }
  17.      {    /* run finite state machine */
  18.     char state = *ps;
  19.     int leave = 0;
  20.     int limit = 0;
  21.     int nout = 0;
  22.     unsigned short wc = wcin;
  23.  
  24.     for (; ; )
  25.         {    /* perform a state transformation */
  26.         unsigned short code;
  27.         const unsigned short *stab;
  28.  
  29.         if (_NSTATE <= state
  30.             || (stab = _Wcstate._Tab[state]) == NULL
  31.             || MB_CUR_MAX <= nout
  32.             || (_NSTATE*UCHAR_MAX) <= ++limit
  33.             || (code = stab[wc & UCHAR_MAX]) == 0)
  34.             break;
  35.         state = (code & ST_STATE) >> ST_STOFF;
  36.         if (code & ST_FOLD)
  37.             wc = wc & ~UCHAR_MAX | code & ST_CH;
  38.         if (code & ST_ROTATE)
  39.             wc = wc >> CHAR_BIT & UCHAR_MAX | wc << CHAR_BIT;
  40.         if (code & ST_OUTPUT)
  41.             {    /* produce an output char */
  42.             if ((s[nout++] = code & ST_CH ? code : wc) == '\0')
  43.                 leave = 1;
  44.             limit = 0;
  45.             }
  46.         if (code & ST_INPUT || leave)
  47.             {    /* consume input */
  48.             *ps = state;
  49.             return (nout);
  50.             }
  51.         }
  52.     *ps = _NSTATE;
  53.     return (-1);
  54.      }
  55.     }
  56.  
  57.  
  58.